#include "support_common.h"

static void busy_delay(int p_delay){
	int i, x = 1;
	for (i = 0; i < p_delay; ++i){
		x = x * 2 + 1;
	}
}

static void led_off(int p_led){
	MCF_GPIO_CLRTC &= ~(1 << (p_led - 1));
}

static void led_on(int p_led){
	MCF_GPIO_SETTC |= 1 << (p_led - 1);
}

int main(){
	MCF_GPIO_PTCPAR = MCF_GPIO_PTCPAR_PTCPAR0(MCF_GPIO_PTCPAR_DTIN0_GPIO) |
			MCF_GPIO_PTCPAR_PTCPAR1(MCF_GPIO_PTCPAR_DTIN1_GPIO) |
			MCF_GPIO_PTCPAR_PTCPAR2(MCF_GPIO_PTCPAR_DTIN2_GPIO) |
			MCF_GPIO_PTCPAR_PTCPAR3(MCF_GPIO_PTCPAR_DTIN3_GPIO);
	
	MCF_GPIO_DDRTC = MCF_GPIO_DDRTC_DDRTC0 | MCF_GPIO_DDRTC_DDRTC1 | MCF_GPIO_DDRTC_DDRTC2
			| MCF_GPIO_DDRTC_DDRTC3;
	
	int number = 0;
	int bit0Mask = 1; //0001
	int bit1Mask = 2; //0010
	int bit2Mask = 4; //0100
	int bit3Mask = 8; //1000
	led_off(1); led_off(2); led_off(3); led_off(4);
	
	while(1){
		//turn everything off
		led_off(1); led_off(2); led_off(3); led_off(4);
		
		busy_delay(500000);
	
		//check each bit in number
		//if bit is 1, turn LED on
		if(bit0Mask & number)
			led_on(1);
		
		if(bit1Mask & number)
			led_on(2);

		if(bit2Mask & number)
			led_on(3);
		
		if(bit3Mask & number)
			led_on(4);
		
		busy_delay(500000);
		
		//increment number
		number++;
		//only four bits, so max number = 15
		if(number > 15)
			number = 0;
	}
	return 0;
}